package com.google.jguery.client.index;

import java.util.Iterator;
import java.util.Set;

import com.google.jguery.client.common.FieldText;
import com.google.jguery.client.common.TermFreq;
import com.google.jguery.client.common.TermPostings;

public class TermReader implements Iterator<TermFreq>{

	Table termTable;
	Iterator<FieldText> iter;
	
	public TermReader(Table termTable) {
		this.termTable = termTable;
		iter = termTable.getKeys().iterator();
	}
	
	public boolean contains(FieldText ft) {
		return termTable.contains(ft);
	}
	
	public int getDocCount(FieldText ft) {
		return termTable.getPostingsDocCount(ft);
	}
	
	public int getTermCount(FieldText ft) {
		return termTable.getPostingsTermCount(ft);
	}

	public Set<FieldText> getAllTerms() {
		return termTable.getKeys();
	}
	
	public TermPostings getPostings(FieldText key) {
		return termTable.getPostings(key);
	}
	
	@Override
	public boolean hasNext() {
		return iter.hasNext();
	}

	@Override
	public TermFreq next() {
		FieldText ft = iter.next();
		TermPostings termPostings = termTable.getPostings(ft);
		if (termPostings == null) {
			return null;
		} else {
			int docCount = termPostings.getDocCount();
			int termCount = termPostings.getTermCount();
			return new TermFreq(ft, docCount, termCount);
		}
	}

	@Override
	public void remove() {
		iter.remove();
	}
}
